/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.net.SocketFactories;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.net.Socket;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.net.ssl.SSLHandshakeException;
/*     */ import javax.net.ssl.SSLSocket;
/*     */ import javax.net.ssl.SSLSocketFactory;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestCiphers
/*     */   extends TestCase
/*     */ {
/*  39 */   private static final String[] EXCLUDED_CIPHERS = { "SSL_RSA_WITH_DES_CBC_SHA" };
/*     */   
/*     */ 
/*     */   private static final boolean TESTING_NIO = false;
/*     */   
/*     */ 
/*     */   private static final boolean zimbraSSLExcludeCipherSuites_configured = true;
/*     */   
/*     */ 
/*     */   private static final String CRLF = "\r\n";
/*     */   
/*     */ 
/*     */   private static final String HOSTNAME = "localhost";
/*     */   
/*     */ 
/*     */   private static final String POP3_CONNECT_RESPONSE = "\\+OK .* POP3 server ready";
/*     */   
/*     */ 
/*     */   private static final String POP3_USER = "USER user1\r\n";
/*     */   
/*     */ 
/*     */   private static final String POP3_USER_RESPONSE = "\\+OK hello user1, please enter your password";
/*     */   
/*     */ 
/*     */   private static final String POP3_PASS = "PASS test123\r\n";
/*     */   
/*     */ 
/*     */   private static final String POP3_PASS_RESPONSE = "\\+OK server ready";
/*     */   
/*     */ 
/*     */   private static final String POP3_STLS = "STLS\r\n";
/*     */   
/*     */ 
/*     */   private static final String POP3_STLS_RESPONSE = "\\+OK Begin TLS negotiation";
/*     */   
/*     */ 
/*     */   private static final String POP3_CLEARTEXT_FAILED_RESPONSE = "-ERR only valid after entering TLS mode";
/*     */   
/*     */ 
/*     */   private static final String POP3_QUIT = "QUIT\r\n";
/*     */   
/*     */ 
/*     */   private static final String POP3_QUIT_RESPONSE = "\\+OK .* closing connection";
/*     */   
/*     */ 
/*     */   private static final String POP3_XOIP = "XOIP 100.99.98.97\r\n";
/*     */   
/*     */   private static final String POP3_XOIP_RESPONSE = "\\+OK";
/*     */   
/*     */   private static final String IMAP_CONNECT_RESPONSE = "\\* OK .* Zimbra IMAP4rev1 service ready";
/*     */   
/*     */   private static final String IMAP_LOGIN = "1 LOGIN user1 test123\r\n";
/*     */   
/*     */   private static final String IMAP_LOGIN_RESPONSE = "1 OK.*LOGIN completed";
/*     */   
/*     */   private static final String IMAP_CLEARTEXT_FAILED_RESPONSE = "1 NO cleartext logins disabled";
/*     */   
/*     */   private static final String IMAP_STARTTLS = "2 STARTTLS\r\n";
/*     */   
/*     */   private static final String IMAP_STARTTLS_RESPONSE = "2 OK Begin TLS negotiation now";
/*     */   
/*     */   private static final String IMAP_STARTTLS_RESPONSE_NIO = "2 OK begin TLS negotiation now";
/*     */   
/*     */   private static final String IMAP_LOGOUT = "3 LOGOUT\r\n";
/*     */   
/*     */   private static final String IMAP_LOGOUT_RESPONSE1 = "\\* BYE.*IMAP4rev1 server terminating connection";
/*     */   
/*     */   private static final String IMAP_LOGOUT_RESPONSE2 = "3 OK LOGOUT completed";
/*     */   
/*     */   private static final String IMAP_ID = "4 ID (\"X-ORIGINATING-IP\" \"100.99.98.97\")\r\n";
/*     */   
/*     */   private static final String IMAP_ID_RESPONSE1 = "\\* ID.*";
/*     */   
/*     */   private static final String IMAP_ID_RESPONSE2 = "4 OK ID completed";
/*     */   
/*     */   private static final String SOAP_ENV = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">  <soap:Header>    <context xmlns=\"urn:zimbra\">      <session></session>    </context>  </soap:Header>  <soap:Body>    <PingRequest xmlns=\"urn:zimbraAdmin\"></PingRequest>  </soap:Body></soap:Envelope>";
/*     */   
/* 116 */   private static final String HTTP_SOAP_PING = "POST /service/admin/soap/ HTTP/1.1\r\nContent-Type: text/xml; charset=utf-8\r\nUser-Agent: Jakarta Commons-HttpClient/3.0\r\nHost: localhost:7070\r\nContent-Length: " + "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">  <soap:Header>    <context xmlns=\"urn:zimbra\">      <session></session>    </context>  </soap:Header>  <soap:Body>    <PingRequest xmlns=\"urn:zimbraAdmin\"></PingRequest>  </soap:Body></soap:Envelope>".length() + "\r\n" + "\r\n" + "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">  <soap:Header>    <context xmlns=\"urn:zimbra\">      <session></session>    </context>  </soap:Header>  <soap:Body>    <PingRequest xmlns=\"urn:zimbraAdmin\"></PingRequest>  </soap:Body></soap:Envelope>";
/*     */   
/*     */ 
/*     */   private static final String HTTP_SOAP_PING_RESPONSE = "HTTP/1.1 200 OK";
/*     */   
/*     */   private static final String HTTP_SOAP_PING_RESPONSE_CLEARTEXT = "HTTP/1.1 500 Internal Server Error";
/*     */   
/*     */   private static int mPop3CleartextPort;
/*     */   
/*     */   private static int mPop3SslPort;
/*     */   
/*     */   private static int mImapCleartextPort;
/*     */   
/*     */   private static int mImapSslPort;
/*     */   
/*     */   private static int mHttpCleartextPort;
/*     */   
/*     */   private static int mHttpSslPort;
/*     */   
/* 135 */   private Map<Socket, BufferedReader> mReaders = new HashMap();
/*     */   
/*     */   static {
/*     */     try {
/* 139 */       init();
/*     */     } catch (Exception e) {
/* 141 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */   
/*     */   static void init() throws Exception {
/* 146 */     SocketFactories.registerProtocols(true);
/* 147 */     Provisioning prov = Provisioning.getInstance();
/* 148 */     Server server = prov.getLocalServer();
/* 149 */     mPop3CleartextPort = server.getIntAttr("zimbraPop3BindPort", 7110);
/* 150 */     mPop3SslPort = server.getIntAttr("zimbraPop3SSLBindPort", 7995);
/* 151 */     mImapCleartextPort = server.getIntAttr("zimbraImapBindPort", 7143);
/* 152 */     mImapSslPort = server.getIntAttr("zimbraImapSSLBindPort", 7995);
/* 153 */     mHttpCleartextPort = server.getIntAttr("zimbraMailPort", 7070);
/* 154 */     mHttpSslPort = server.getIntAttr("zimbraAdminPort", 7071);
/*     */   }
/*     */   
/*     */   private void setEnabledCipherSuites(Socket socket) {
/* 158 */     SSLSocket sslSocket = (SSLSocket)socket;
/* 159 */     sslSocket.setEnabledCipherSuites(EXCLUDED_CIPHERS);
/*     */   }
/*     */   
/*     */   public void pop3(boolean useExcludedCipher) throws Exception
/*     */   {
/* 164 */     Socket socket = new Socket("localhost", mPop3CleartextPort);
/* 165 */     send(socket, "", "\\+OK .* POP3 server ready");
/* 166 */     send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 167 */     send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 168 */     send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 169 */     socket.close();
/*     */     
/*     */ 
/* 172 */     boolean good = false;
/* 173 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", mPop3SslPort);
/* 174 */     if (useExcludedCipher)
/* 175 */       setEnabledCipherSuites(socket);
/*     */     try {
/* 177 */       send(socket, "", "\\+OK .* POP3 server ready");
/* 178 */       send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 179 */       send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 180 */       send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 181 */       good = !useExcludedCipher;
/*     */     } catch (SSLHandshakeException e) {
/* 183 */       good = useExcludedCipher;
/*     */     } finally {
/* 185 */       socket.close();
/*     */     }
/* 187 */     assertTrue(good);
/*     */     
/*     */ 
/* 190 */     good = false;
/* 191 */     socket = new Socket("localhost", mPop3CleartextPort);
/* 192 */     send(socket, "", "\\+OK .* POP3 server ready");
/* 193 */     send(socket, "STLS\r\n", "\\+OK Begin TLS negotiation");
/* 194 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", mPop3CleartextPort, true);
/* 195 */     if (useExcludedCipher)
/* 196 */       setEnabledCipherSuites(socket);
/*     */     try {
/* 198 */       send(socket, "USER user1\r\n", "\\+OK hello user1, please enter your password");
/* 199 */       send(socket, "PASS test123\r\n", "\\+OK server ready");
/* 200 */       send(socket, "QUIT\r\n", "\\+OK .* closing connection");
/* 201 */       good = !useExcludedCipher;
/*     */     } catch (SSLHandshakeException e) {
/* 203 */       good = useExcludedCipher;
/*     */     } finally {
/* 205 */       socket.close();
/*     */     }
/* 207 */     assertTrue(good);
/*     */   }
/*     */   
/*     */   public void imap(boolean useExcludedCipher) throws Exception
/*     */   {
/* 212 */     Socket socket = new Socket("localhost", mImapCleartextPort);
/* 213 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 service ready");
/* 214 */     send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 215 */     send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server terminating connection");
/* 216 */     send(socket, null, "3 OK LOGOUT completed");
/*     */     
/*     */ 
/* 219 */     boolean good = false;
/* 220 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", mImapSslPort);
/* 221 */     if (useExcludedCipher)
/* 222 */       setEnabledCipherSuites(socket);
/*     */     try {
/* 224 */       send(socket, null, "\\* OK .* Zimbra IMAP4rev1 service ready");
/* 225 */       send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 226 */       send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server terminating connection");
/* 227 */       send(socket, null, "3 OK LOGOUT completed");
/* 228 */       good = !useExcludedCipher;
/*     */     } catch (SSLHandshakeException e) {
/* 230 */       good = useExcludedCipher;
/*     */     } finally {
/* 232 */       socket.close();
/*     */     }
/* 234 */     assertTrue(good);
/*     */     
/*     */ 
/* 237 */     good = false;
/* 238 */     socket = new Socket("localhost", mImapCleartextPort);
/* 239 */     send(socket, null, "\\* OK .* Zimbra IMAP4rev1 service ready");
/* 240 */     String expectedStartTLSResp = "2 OK Begin TLS negotiation now";
/* 241 */     send(socket, "2 STARTTLS\r\n", expectedStartTLSResp);
/* 242 */     socket = SocketFactories.dummySSLSocketFactory().createSocket(socket, "localhost", mImapCleartextPort, true);
/* 243 */     if (useExcludedCipher)
/* 244 */       setEnabledCipherSuites(socket);
/*     */     try {
/* 246 */       send(socket, "1 LOGIN user1 test123\r\n", "1 OK.*LOGIN completed");
/* 247 */       send(socket, "3 LOGOUT\r\n", "\\* BYE.*IMAP4rev1 server terminating connection");
/* 248 */       send(socket, null, "3 OK LOGOUT completed");
/* 249 */       good = !useExcludedCipher;
/*     */     } catch (SSLHandshakeException e) {
/* 251 */       good = useExcludedCipher;
/*     */     } finally {
/* 253 */       socket.close();
/*     */     }
/* 255 */     assertTrue(good);
/*     */   }
/*     */   
/*     */   public void http(boolean useExcludedCipher) throws Exception
/*     */   {
/* 260 */     Socket socket = new Socket("localhost", mHttpCleartextPort);
/* 261 */     send(socket, HTTP_SOAP_PING, "HTTP/1.1 500 Internal Server Error");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 267 */     boolean good = false;
/* 268 */     socket = SocketFactories.dummySSLSocketFactory().createSocket("localhost", mHttpSslPort);
/* 269 */     if (useExcludedCipher)
/* 270 */       setEnabledCipherSuites(socket);
/*     */     try {
/* 272 */       send(socket, HTTP_SOAP_PING, "HTTP/1.1 200 OK");
/* 273 */       good = !useExcludedCipher;
/*     */     } catch (SSLHandshakeException e) {
/* 275 */       good = useExcludedCipher;
/*     */     } finally {
/* 277 */       socket.close();
/*     */     }
/* 279 */     assertTrue(good);
/*     */   }
/*     */   
/*     */   public void testPop3() throws Exception {
/* 283 */     pop3(true);
/* 284 */     pop3(false);
/*     */   }
/*     */   
/*     */   public void testImap() throws Exception {
/* 288 */     imap(true);
/* 289 */     imap(false);
/*     */   }
/*     */   
/*     */   public void testHttp() throws Exception {
/* 293 */     http(true);
/* 294 */     http(false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void send(Socket socket, String msg, String responsePattern)
/*     */     throws Exception
/*     */   {
/* 307 */     if (msg != null) {
/* 308 */       OutputStream out = socket.getOutputStream();
/* 309 */       out.write(msg.getBytes());
/* 310 */       out.flush();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 316 */     BufferedReader reader = (BufferedReader)this.mReaders.get(socket);
/* 317 */     if (reader == null) {
/* 318 */       reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/* 319 */       this.mReaders.put(socket, reader);
/*     */     }
/*     */     
/* 322 */     String response = reader.readLine();
/* 323 */     String errorMsg = "Unexpected response: '" + response + "'";
/* 324 */     assertTrue(errorMsg, response.matches(responsePattern));
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 328 */     TestUtil.cliSetup();
/* 329 */     TestUtil.runTest(TestCiphers.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestCiphers.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */